package com.service;

import com.bean.PostImg;
import com.bean.PostMessage;
import com.bean.User;
import com.config.MinioAddr;
import com.mapper.PostImgMapper;
import com.mapper.PostMessageMapper;
import com.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mapper.PostMapper;
import com.bean.Post;
import com.service.PostService;
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService{

    @Autowired
    PostMapper postMapper;

    @Autowired
    PostImgMapper postImgMapper;

    @Autowired
    PostMessageMapper postMessageMapper;

    @Autowired
    UserMapper userMapper;


    @Override
    public List<Map<String, Object>> getPostsByUserId(int userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        List<Post> posts = postMapper.selectByMap(map);

        List<Map<String, Object>> outputList = new ArrayList<>();
        for (Post post : posts) {
            Map<String, Object> postMap = new HashMap<>();
            // 根据宝贝id查询图片，仅要第一张
            postMap.put("post_id", post.getId());
            postMap.put("no", 1);

            Map<String, Object> outputData = new HashMap<>();

            List<PostImg> postImgs = postImgMapper.selectByMap(postMap);
            if (postImgs.size() != 0) {
                outputData.put("img", MinioAddr.IMG_PATH + postImgs.get(0).getSrc());
            }
            outputData.put("id", post.getId());
            outputData.put("text", post.getText());
            outputData.put("userId", post.getUserId());
            outputData.put("date", post.getDate());

            outputList.add(outputData);
        }


        return outputList;
    }

    @Override
    public int insertPost(Post post) {
        return postMapper.insert(post);
    }

    /**
     * 根据user id获取该用户发布的所有帖子
     *
     * @param userId
     * @return
     */
    @Override
    public List<Post> getPostInfoByUserId(int userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);

        return postMapper.selectByMap(map);
    }

    /**
     * 根据post id 获取该帖子的页面信息
     *
     * @param postId
     * @return
     */
    @Override
    public Map<String, Object> getPostInfoByPostId(int postId) {
        // 将要返回的map
        // 其中包含 宝贝详情 宝贝的图片 宝贝的评论
        Map<String, Object> outputmap = new HashMap<>();

        // ********************************************
        // 1、获取宝贝信息
        Post post = postMapper.selectById(postId);
        outputmap.put("post", post);

        // ********************************************
        // 2、获取宝贝图片
        Map<String, Object> map = new HashMap<>();
        map.put("post_id", postId);
        List<PostImg> postImgs = postImgMapper.selectByMap(map);

        List<Map<String, Object>> postImgsList = new ArrayList<>();
        for (PostImg postImg : postImgs) {
            // 仅获取 no 和 src 即可
            Map<String, Object> postImgMap = new HashMap<>();
            postImgMap.put("no", postImg.getNo());
            postImgMap.put("src", MinioAddr.IMG_PATH + postImg.getSrc());
            postImgsList.add(postImgMap);
        }
        outputmap.put("postImg", postImgsList);

        // ********************************************
        // 3、获取宝贝评论 条件同上map
        List<PostMessage> postMessages = postMessageMapper.selectByMap(map);

        // 将获取的宝贝评论按要返回的内容封装成一个新的list
        List<Map<String, Object>> postMessagesList = new ArrayList<>();
        for (PostMessage postMessage : postMessages) {
            // 填充宝贝信息
            Map<String, Object> goodMessageMap = new HashMap<>();
            goodMessageMap.put("no", postMessage.getNo());
            goodMessageMap.put("fatherNo", postMessage.getFatherNo());
            goodMessageMap.put("userId", postMessage.getUserId());
            goodMessageMap.put("date", postMessage.getDate());
            goodMessageMap.put("text", postMessage.getText());
            goodMessageMap.put("sendType", postMessage.getSendType());
            goodMessageMap.put("sendToUserId", postMessage.getSendToUserId());
            goodMessageMap.put("sendToUserNickName", postMessage.getSendToUserNickName());
            goodMessageMap.put("messageChecked", postMessage.getMessageChecked());

            // 获取用户信息
            User user = userMapper.selectById(postMessage.getUserId());
            goodMessageMap.put("icon", MinioAddr.IMG_PATH + user.getIcon());
            goodMessageMap.put("nickName", user.getNickName());

            postMessagesList.add(goodMessageMap);
        }

        // 将留言按其父子关系封装成新的list
        List<Map<String, Object>> postMessagesList2 = new ArrayList<>();
        for (Map<String, Object> postMessage : postMessagesList) {
            // 判断是否为父留言
            if ((int)postMessage.get("fatherNo") == 0) {
                // 遍历留言list 如果留言的fatherNo 和 父留言的no相同则添加进sonMessages
                List<Map<String, Object>> sonMessages = new ArrayList<>();
                for (int i = 0; i < postMessagesList.size(); i++) {
                    if (postMessagesList.get(i).get("fatherNo") == postMessage.get("no")) {
                        sonMessages.add(postMessagesList.get(i));
                    }
                }

                postMessage.put("sonMessages", sonMessages);

                postMessagesList2.add(postMessage);
            }

        }
        outputmap.put("postMessages", postMessagesList2);

        return outputmap;
    }

    @Override
    public Map<String, Object> getPageOwnInfoByPostId(int post_id) {
        Post post = postMapper.selectById(post_id);
        User user = userMapper.selectById(post.getUserId());

        Map<String, Object> pageOwner = new HashMap<>();

        pageOwner.put("id", user.getId());
        pageOwner.put("nickName", user.getNickName());
        pageOwner.put("icon", MinioAddr.IMG_PATH + user.getIcon());
        pageOwner.put("sign", user.getSign());


        return pageOwner;
    }

    @Override
    public int countGoodMessagesByGoodId(int postId) {
        Map<String, Object> map = new HashMap<>();
        map.put("post_id", postId);
        List<PostMessage> postMessages = postMessageMapper.selectByMap(map);

        return postMessages.size();
    }

    @Override
    public int addPostMessage(PostMessage postMessage) {
        return postMessageMapper.insert(postMessage);
    }

}
